﻿using System;
using System.Collections.Generic;
using UnityEngine;

namespace UFrame
{
    public class PLGUITools : MonoBehaviour
    {
        private static Dictionary<int, GUIUIBase> dic = new Dictionary<int, GUIUIBase>();
        private static Dictionary<string, GUIUIBase> strDic = new Dictionary<string, GUIUIBase>();
        private static int Count = 0;

        private static GUIStyle curStyle = new GUIStyle();
        private GUIStyle black = new GUIStyle();
        private GUIStyle white = new GUIStyle();

        public bool isShow = true;
        
        public void SetFontStyle(int style)
        {
            if (style == 0)
            {
            }
            else
            {
            }
        }

        public void Awake()
        {
            black.fontSize = 38;
            black.normal.textColor = Color.black;
            curStyle = black;
        }

        public void OnGUI()
        {
            if (isShow)
            {
                GUISus();
            }
        }

        private void GUISus()
        {
            if (dic.Count > 0)
            {
                foreach (var item in dic)
                {
                    DoType(item.Value);
                }
            }

            if (strDic.Count > 0)
            {
                foreach (var item in strDic)
                {
                    DoType(item.Value);
                }
            }
        }


        public static int AddUI(GUIType type, object o)
        {
            Count++;
            dic.Add(Count, new GUIUIBase() {parameter = o, type = type});
            return Count;
        }

        public static GUIUIBase ShowUI(int id, GUIType type, object o)
        {
            if (dic.ContainsKey(id))
            {
                dic[id].parameter = o;
                dic[id].type = type;
                return dic[id];
            }
            else
            {
                var guiData = new GUIUIBase() {parameter = o, type = type};
                dic.Add(id, guiData);
                return guiData;
            }
        }

        public static GUIUIBase ShowUI(int id, object o)
        {
            GUIType gy = GUIType.Label;
            return ShowUI(id, gy, o);
        }

        public static GUIUIBase ShowUI(string title, object o)
        {
            GUIType gy = GUIType.Label;

            if (strDic.ContainsKey(title))
            {
                var strData = strDic[title];
                strData.parameter = o;
                return strData;
            }
            else
            {
                var guiData = new GUIUIBase() {parameter = o, type = gy};
                guiData.title = title;
                strDic.Add(title, guiData);
                return guiData;
            }
        }


        public static int GetId()
        {
            return Count++;
        }


        public static void RemoveUI(int id)
        {
            if (dic.ContainsKey(id))
            {
                dic.Remove(id);
            }
        }

        public static object GetParameter(int id, bool reset = false)
        {
            if (reset)
            {
                var o = dic[id].parameter;
                dic[id].parameter = null;
                return o;
            }
            else
            {
                return dic[id].parameter;
            }
        }

        public static void SetParameter(int id, object o)
        {
            dic[id].parameter = o;
        }


        private static void DoType(GUIUIBase data)
        {
            switch (data.type)
            {
                case GUIType.TextField:
                    data.parameter = GUILayout.TextField(data.title + ": " + (string) data.parameter, curStyle);
                    break;
                case GUIType.Label:
                    if (data.parameter is ValueType)
                    {
                        GUILayout.Label(data.title + ": " + Convert.ToString(data.parameter), curStyle);
                    }
                    else
                    {
                        GUILayout.Label(data.title + ": " + (string) data.parameter, curStyle);
                    }
                    break;
                case GUIType.Button:
                    data.parameter = GUILayout.Button((string) data.parameter, curStyle);
                    break;
                default:
                    break;
            }
        }

        public class GUIUIBase
        {
            public GUIType type;
            public object parameter;
            public string title;
        }
    }

    public enum GUIType
    {
        TextField,
        Label,
        Button,
    }
}